package com.itheima.service.impl;

import com.itheima.entity.SysConfig;
import com.itheima.mapper.ConfigMapper;
import com.itheima.service.ConfigService;
import com.itheima.vo.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ConfigServiceImpl implements ConfigService {
    private final ConfigMapper configMapper;

    /**
     * 所有配置数据
     *
     * @return {@link ConfigAllVO}
     */
    @Override
    public ConfigAllVO all() {

        ConfigAllVO configAllVO = new ConfigAllVO();

        List<ListVO> listVOList = new ArrayList<>(); //有3个listVO,基础配置,...

        ListVO listVO1 = new ListVO();//第一个listVO
        listVO1.setName("基础配置");
        listVO1.setKey("base");

        List<ChildrenVO> childrenVOList = new ArrayList<>();//有3个childrenVO,站点配置,底部信息,其他配置

        ChildrenVO children1 = new ChildrenVO();//第一个ChildrenVO,站点配置
        children1.setName("站点配置");
        List<SysConfig> sysConfigList1 = configMapper.selectByGroupValue(children1.getName());
        List<ConfigVO2> configVO2List1 = sysConfigList1.stream().map(sysConfig -> {
            ConfigVO2 configVO2 = sysConfig.transformConfigVO2();
            return configVO2;
        }).collect(Collectors.toList());
        children1.setData(configVO2List1);
        childrenVOList.add(children1);
        //childrenVOList.add(children1);

        ChildrenVO children2 = new ChildrenVO();//第一个ChildrenVO,底部信息
        //List<ChildrenVO> children2 = listVOList.get(1).getChildren();
        children2.setName("底部信息");
        List<SysConfig> sysConfigList2 = configMapper.selectByGroupValue(children2.getName());
        List<ConfigVO2> configVO2List2 = sysConfigList2.stream().map(sysConfig -> {
            ConfigVO2 configVO2 = sysConfig.transformConfigVO2();
            return configVO2;
        }).collect(Collectors.toList());
        children2.setData(configVO2List2);
        childrenVOList.add(children2);

        //List<ChildrenVO> children3 = listVOList.get(2).getChildren();
        ChildrenVO children3 = new ChildrenVO();//第三个ChildrenVO,其他配置
        children3.setName("其他配置");
        List<SysConfig> sysConfigList3 = configMapper.selectByGroupValue(children3.getName());
        List<ConfigVO2> configVO2List3 = sysConfigList3.stream().map(sysConfig -> {
            ConfigVO2 configVO2 = sysConfig.transformConfigVO2();
            return configVO2;
        }).collect(Collectors.toList());
        children3.setData(configVO2List3);
        childrenVOList.add(children3);

        listVO1.setChildren(childrenVOList);//第一个listVO1

        ListVO listVO2 = new ListVO();//第二个listVO2
        listVO2.setName("系统设置");
        listVO2.setKey("system");
        List<ChildrenVO> childrenVOList1 = new ArrayList<>();
        //List<ChildrenVO> children2 = listVOList.get(1).getChildren();
        ChildrenVO children4 = new ChildrenVO();//第一个ChildrenVO,管理系统配置
        children4.setName("管理系统配置");
        List<SysConfig> sysConfigList4 = configMapper.selectByGroupValue(children4.getName());
        List<ConfigVO2> configVO2List4 = sysConfigList4.stream().map(sysConfig -> {
            ConfigVO2 configVO2 = sysConfig.transformConfigVO2();
            return configVO2;
        }).collect(Collectors.toList());
        children4.setData(configVO2List4);
        childrenVOList1.add(children4);
        listVO2.setChildren(childrenVOList1);//第二个listVO2完成

        ListVO listVO3 = new ListVO();//第三个listVO3开始
        listVO3.setName("其他设置");
        listVO3.setKey("custom");
        List<ChildrenVO> childrenVOList2 = new ArrayList<>();
        //List<ChildrenVO> children3 = listVOList.get(3).getChildren();
        ChildrenVO children5 = new ChildrenVO();//第一个ChildrenVO
        children5.setName("通知公告");
        List<SysConfig> sysConfigList5 = configMapper.selectByGroupValue(children5.getName());
        List<ConfigVO2> configVO2List5 = sysConfigList5.stream().map(sysConfig -> {
            ConfigVO2 configVO2 = sysConfig.transformConfigVO2();
            return configVO2;
        }).collect(Collectors.toList());
        children5.setData(configVO2List5);
        childrenVOList2.add(children5);


        ChildrenVO children6 = new ChildrenVO();//第二个ChildrenVO
        children6.setName("前台图片");
        List<SysConfig> sysConfigList6 = configMapper.selectByGroupValue(children6.getName());
        List<ConfigVO2> configVO2List6 = sysConfigList6.stream().map(sysConfig -> {
            ConfigVO2 configVO2 = sysConfig.transformConfigVO2();
            return configVO2;
        }).collect(Collectors.toList());
        children6.setData(configVO2List6);
        childrenVOList2.add(children6);
        listVO3.setChildren(childrenVOList2);//第三个listVO3完成

        listVOList.add(listVO1);
        listVOList.add(listVO2);
        listVOList.add(listVO3);

        configAllVO.setList(listVOList);

        //todo 获取allData
        List<SysConfig> sysConfigList = configMapper.selectAllData();
        List<ConfigVO2> configVO2s = sysConfigList.stream().map(new Function<SysConfig, ConfigVO2>() {
            @Override
            public ConfigVO2 apply(SysConfig sysConfig) {
                ConfigVO2 configVO2 = sysConfig.transformConfigVO2();
                return configVO2;
            }
        }).collect(Collectors.toList());
        configAllVO.setAllData(configVO2s);


        List<String> values = configMapper.selectValue();
        ConfigFormVO configFormVO = new ConfigFormVO();
        configFormVO.setNotic_title(values.get(0));
        configFormVO.setSite_name(values.get(1));
        configFormVO.setSite_link(values.get(2));
        configFormVO.setSite_keywords(values.get(3));
        configFormVO.setSite_description(values.get(4));
        configFormVO.setLoginreg_bg(values.get(5));
        configFormVO.setCopy_code(values.get(6));
        configFormVO.setCopy_info(values.get(7));
        configFormVO.setPhone(values.get(8));
        configFormVO.setCopy_url(values.get(9));
        configFormVO.setBlack_ip(values.get(10));
        configFormVO.setLogin_title(values.get(11));
        configFormVO.setLogin_bg(values.get(12));
        configFormVO.setSystem_name(values.get(13));
        configFormVO.setSystem_logo(values.get(14));
        configFormVO.setWeb_ico(values.get(15));
        configFormVO.setClose_website(values.get(16));
        configFormVO.setWeb_img(values.get(17));
        configFormVO.setNotic_content(values.get(18));


        /*List<HashMap<String, String>> configFormList = configMapper.selectConfig();
        ConfigFormVO configFormVO = new ConfigFormVO();
        for (HashMap<String, String> conf : configFormList) {
            conf.get
        }*/

        /*configFormVO.setSystem_name(map.get("system_name"));
        configFormVO.setSystem_logo(map.get("system_logo"));
        configFormVO.setWeb_ico(map.get("web_ico"));
        configFormVO.setSite_name(map.get("site_name"));
        configFormVO.setSite_link(map.get("site_link"));
        configFormVO.setSite_keywords(map.get("site_keywords"));
        configFormVO.setClose_website(map.get("close_website"));
        configFormVO.setCopy_code(map.get("copy_code"));
        configFormVO.setCopy_info(map.get("copy_info"));
        configFormVO.setPhone(map.get("phone"));
        configFormVO.setCopy_url(map.get("copy_url"));
        configFormVO.setBlack_ip(map.get("black_ip"));
        configFormVO.setLogin_title(map.get("login_title"));
        configFormVO.setLogin_bg(map.get("login_bg"));*/

        configAllVO.setConfigForm(configFormVO);


        String site_name = configMapper.selectValidatorBySiteName();
        ConfigRulesVO configRulesVO = new ConfigRulesVO();
        configRulesVO.setSite_name(site_name);
        configAllVO.setConfigRules(configRulesVO);



















        /*ConfigAllVO configAllVO = new ConfigAllVO();
        List<ListVO> listVOList = new ArrayList<>(3);

        //ListVO listVO1 = new ListVO();
        listVOList.get(0).setName("基础配置");
        listVOList.get(0).setKey("base");
        List<ChildrenVO> children1 = listVOList.get(0).getChildren();

        children1.get(0).setName("站点配置");
        List<ConfigVO2> configVO2List1 = configMapper.selectByGroupValue(children1.get(0).getName());
        children1.get(0).setData(configVO2List1);

        //List<ChildrenVO> children2 = listVOList.get(1).getChildren();
        children1.get(1).setName("底部信息");
        List<ConfigVO2> configVO2List2 = configMapper.selectByGroupValue(children1.get(1).getName());
        children1.get(1).setData(configVO2List2);

        //List<ChildrenVO> children3 = listVOList.get(2).getChildren();
        children1.get(2).setName("其他配置");
        List<ConfigVO2> configVO2List3 = configMapper.selectByGroupValue(children1.get(2).getName());
        children1.get(2).setData(configVO2List3);

        listVOList.get(1).setName("系统设置");
        listVOList.get(1).setKey("system");
        List<ChildrenVO> children2 = listVOList.get(1).getChildren();
        children2.get(0).setName("管理系统配置");
        List<ConfigVO2> configVO2List4 = configMapper.selectByGroupValue(children2.get(0).getName());
        children2.get(0).setData(configVO2List4);


        listVOList.get(2).setName("其他设置");
        listVOList.get(2).setKey("custom");
        List<ChildrenVO> children3 = listVOList.get(3).getChildren();
        children3.get(0).setName("通知公告");
        List<ConfigVO2> configVO2List5 = configMapper.selectByGroupValue(children3.get(0).getName());
        children3.get(0).setData(configVO2List5);
        children3.get(1).setName("前台图片");
        List<ConfigVO2> configVO2List6 = configMapper.selectByGroupValue(children3.get(1).getName());
        children3.get(1).setData(configVO2List6);

        configAllVO.setList(listVOList);


        List<SysConfig> configVO2List = configMapper.selectAllData();
        List<ConfigVO2> configVO2s = configVO2List.stream().map(new Function<SysConfig, ConfigVO2>() {
            @Override
            public ConfigVO2 apply(SysConfig sysConfig) {
                ConfigVO2 configVO2 = sysConfig.transformConfigVO2();
                return configVO2;
            }
        }).collect(Collectors.toList());
        configAllVO.setAllData(configVO2s);


        HashMap<String, String> map = configMapper.selectConfig();
        ConfigFormVO configFormVO = new ConfigFormVO();
        configFormVO.setSystem_name(map.get("system_name"));
        configFormVO.setSystem_logo(map.get("system_logo"));
        configFormVO.setWeb_ico(map.get("web_ico"));
        configFormVO.setSite_name(map.get("site_name"));
        configFormVO.setSite_link(map.get("site_link"));
        configFormVO.setSite_keywords(map.get("site_keywords"));
        configFormVO.setClose_website(map.get("close_website"));
        configFormVO.setCopy_code(map.get("copy_code"));
        configFormVO.setCopy_info(map.get("copy_info"));
        configFormVO.setPhone(map.get("phone"));
        configFormVO.setCopy_url(map.get("copy_url"));
        configFormVO.setBlack_ip(map.get("black_ip"));
        configFormVO.setLogin_title(map.get("login_title"));
        configFormVO.setLogin_bg(map.get("login_bg"));
        
        configAllVO.setConfigForm(configFormVO);


        String site_name = configMapper.selectValidatorBySiteName();
        ConfigRulesVO configRulesVO = new ConfigRulesVO();
        configRulesVO.setSite_name(site_name);
        configAllVO.setConfigRules(configRulesVO);*/

        return configAllVO;
    }
}
